import Cookies from "js-cookie";
const cookies = {};
const defaultCookieSetting = {
  domain: window.location.hostname
}

/**
 * @description 存储 cookie 值
 * @param {String} name cookie name
 * @param {String} value cookie value
 * @param {Object} setting cookie setting
 */
cookies.set = (name = "default", value = "", setting = {}) => {
  setting = Object.assign(defaultCookieSetting, setting);
  Cookies.set(`${name}`, value, setting);
};

/**
 * @description 拿到 cookie 值
 * @param {String} name cookie name
 */
cookies.get = (name = "default", setting = {}) => {
  setting = Object.assign(defaultCookieSetting, setting);
  return Cookies.get(`${name}`, setting);
};

/**
 * @description 拿到 cookie 全部的值
 */
cookies.getAll = () => {
  return Cookies.get();
};

/**
 * @description 删除 cookie
 * @param {String} name cookie name
 * @param {Object} setting cookie setting
 */
cookies.remove = (name = "default", setting = {}) => {
  setting = Object.assign(defaultCookieSetting, setting);
  return Cookies.remove(`${name}`, setting);
};

/**
 * @description 删除 所有cookie
 * @param {Object} setting cookie setting
 */
cookies.removeAll = (setting = {}) => {
  const arr = cookies.getAll();
  setting = Object.assign(defaultCookieSetting, setting);
  for (const key in arr) {
    cookies.remove(key, setting);
  }
};

export default cookies;
